<template>
  <el-container class="main-layout">
    <!-- 侧边栏 -->
    <el-aside :width="sidebarCollapsed ? '64px' : '240px'" class="sidebar">
      <div class="sidebar-header">
        <div class="logo">
          <div v-if="!sidebarCollapsed" class="logo-icon">A</div>
          <span v-if="!sidebarCollapsed" class="logo-text">Admin2025</span>
          <div v-else class="logo-collapsed">A</div>
        </div>
      </div>
      
      <el-menu
        :default-active="$route.path"
        class="sidebar-menu"
        :collapse="sidebarCollapsed"
        :collapse-transition="false"
        router
      >
        <!-- 工作台 -->
        <div class="menu-group" v-if="!sidebarCollapsed">
          <span class="menu-group-title">工作台</span>
        </div>
        
        <el-menu-item index="/home">
          <el-icon><Odometer /></el-icon>
          <span>仪表盘</span>
        </el-menu-item>
        
        <el-menu-item index="/home/analytics">
          <el-icon><TrendCharts /></el-icon>
          <span>分析页</span>
        </el-menu-item>
        
        <el-menu-item index="/home/monitoring">
          <el-icon><Monitor /></el-icon>
          <span>监控页</span>
        </el-menu-item>

        <!-- 开发工具 -->
        <div class="menu-group" v-if="!sidebarCollapsed">
          <span class="menu-group-title">开发工具</span>
        </div>
        
        <el-menu-item index="/home/template-center">
          <el-icon><Grid /></el-icon>
          <span>模板中心</span>
        </el-menu-item>
        
        <el-menu-item index="/home/component-center">
          <el-icon><Histogram /></el-icon>
          <span>组件中心</span>
        </el-menu-item>

        <!-- 系统管理 -->
        <div class="menu-group" v-if="!sidebarCollapsed">
          <span class="menu-group-title">系统管理</span>
        </div>
        
        <el-menu-item index="/home/users">
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
        
        <el-menu-item index="/home/roles">
          <el-icon><UserFilled /></el-icon>
          <span>角色管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主内容区 -->
    <el-container class="main-container">
      <!-- 顶部导航 -->
      <el-header class="header">
        <div class="header-left">
          <el-button 
            :icon="Fold" 
            circle 
            @click="toggleSidebar"
            class="collapse-btn"
          />
          
          <!-- 面包屑导航 -->
          <el-breadcrumb separator="/" class="breadcrumb">
            <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-if="$route.meta.title">
              {{ $route.meta.title }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="header-right">
          <!-- 搜索框 -->
          <el-input
            v-model="searchText"
            placeholder="搜索..."
            prefix-icon="Search"
            size="small"
            class="search-input"
            clearable
          />
          
          <!-- 主题切换 -->
          <el-tooltip content="主题切换" placement="bottom">
            <el-button circle @click="toggleTheme">
              <el-icon><Sunny /></el-icon>
            </el-button>
          </el-tooltip>
          
          <!-- 消息通知 -->
          <el-badge :value="unreadCount" :hidden="unreadCount === 0" class="notification-badge">
            <el-button circle>
              <el-icon><Bell /></el-icon>
            </el-button>
          </el-badge>
          
          <!-- 用户信息 -->
          <el-dropdown @command="handleUserCommand">
            <div class="user-info">
              <el-avatar :size="32" :src="userStore.avatar">
                {{ userStore.username?.charAt(0)?.toUpperCase() || 'U' }}
              </el-avatar>
              <span class="username">{{ userStore.username || '用户' }}</span>
              <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人资料
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>
                  系统设置
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon style="color: #f56c6c"><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 页面内容 -->
      <el-main class="main-content">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Odometer,
  TrendCharts,
  Monitor,
  Grid,
  Histogram,
  User,
  UserFilled,
  Fold,
  Search,
  Sunny,
  Bell,
  ArrowDown,
  Setting,
  SwitchButton
} from '@element-plus/icons-vue'

// 响应式数据
const sidebarCollapsed = ref(false)
const searchText = ref('')
const unreadCount = ref(5)
const isDarkTheme = ref(false)

// Store 和 Router
const userStore = useUserStore()
const router = useRouter()

// 切换侧边栏
const toggleSidebar = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value
}

// 切换主题
const toggleTheme = () => {
  isDarkTheme.value = !isDarkTheme.value
  // 这里可以实现主题切换逻辑
  ElMessage.info(isDarkTheme.value ? '已切换到深色主题' : '已切换到浅色主题')
}

// 处理用户下拉菜单命令
const handleUserCommand = (command) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人资料功能开发中...')
      break
    case 'settings':
      ElMessage.info('系统设置功能开发中...')
      break
    case 'logout':
      ElMessageBox.confirm('确定要退出登录吗？', '确认退出', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userStore.logout()
        router.push('/login')
        ElMessage.success('已成功退出登录')
      }).catch(() => {
        ElMessage.info('已取消退出')
      })
      break
  }
}
</script>

<style scoped lang="scss">
.main-layout {
  height: 100vh;
  
  .sidebar {
    background: linear-gradient(180deg, #1e293b 0%, #334155 100%);
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
    transition: width 0.3s ease;
    
    .sidebar-header {
      height: 64px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-bottom: 1px solid rgba(255, 255, 255, 0.1);
      
      .logo {
        display: flex;
        align-items: center;
        color: white;
        
        .logo-icon {
          width: 32px;
          height: 32px;
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          border-radius: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 16px;
          font-weight: 600;
          color: white;
          margin-right: 12px;
        }
        
        .logo-text {
          font-size: 18px;
          font-weight: 600;
        }
        
        .logo-collapsed {
          width: 32px;
          height: 32px;
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          border-radius: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 16px;
          font-weight: 600;
          color: white;
        }
      }
    }
    
    .menu-group {
      padding: 16px 20px 8px;
      
      .menu-group-title {
        font-size: 12px;
        color: rgba(255, 255, 255, 0.6);
        text-transform: uppercase;
        letter-spacing: 1px;
      }
    }
    
    .sidebar-menu {
      background: transparent;
      border: none;
      
      :deep(.el-menu-item) {
        color: rgba(255, 255, 255, 0.8);
        margin: 4px 12px;
        border-radius: 8px;
        height: 44px;
        line-height: 44px;
        
        &:hover {
          background: rgba(255, 255, 255, 0.1);
          color: white;
        }
        
        &.is-active {
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          color: white;
        }
        
        .el-icon {
          margin-right: 12px;
          font-size: 16px;
        }
      }
      
      &.el-menu--collapse {
        :deep(.el-menu-item) {
          padding: 0 20px;
          
          .el-icon {
            margin-right: 0;
          }
        }
      }
    }
  }
  
  .main-container {
    flex: 1;
    
    .header {
      background: white;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 24px;
      height: 64px;
      
      .header-left {
        display: flex;
        align-items: center;
        gap: 16px;
        
        .collapse-btn {
          color: #606266;
        }
        
        .breadcrumb {
          :deep(.el-breadcrumb__inner) {
            color: #606266;
            
            &.is-link {
              color: #409eff;
              
              &:hover {
                color: #66b1ff;
              }
            }
          }
        }
      }
      
      .header-right {
        display: flex;
        align-items: center;
        gap: 16px;
        
        .search-input {
          width: 200px;
        }
        
        .notification-badge {
          :deep(.el-badge__content) {
            background: #f56c6c;
          }
        }
        
        .user-info {
          display: flex;
          align-items: center;
          gap: 8px;
          cursor: pointer;
          padding: 8px 12px;
          border-radius: 8px;
          transition: background-color 0.3s ease;
          
          &:hover {
            background: #f0f2f5;
          }
          
          .username {
            font-size: 14px;
            color: #606266;
            font-weight: 500;
          }
          
          .dropdown-icon {
            font-size: 12px;
            color: #909399;
          }
        }
      }
    }
    
    .main-content {
      background: #f5f7fa;
      padding: 0;
      overflow: auto;
      
      .fade-enter-active,
      .fade-leave-active {
        transition: opacity 0.3s ease;
      }
      
      .fade-enter-from,
      .fade-leave-to {
        opacity: 0;
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .main-layout {
    .sidebar {
      width: 64px !important;
      
      .menu-group {
        display: none;
      }
    }
    
    .header {
      padding: 0 16px;
      
      .header-left {
        .breadcrumb {
          display: none;
        }
      }
      
      .header-right {
        gap: 8px;
        
        .search-input {
          width: 120px;
        }
        
        .user-info {
          .username {
            display: none;
          }
        }
      }
    }
  }
}
</style>
